﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Collections.Specialized;

namespace djs_mp3_manager.Nastroje.MoznostiTabs
{
    public partial class Folders : UserControl,ISettingable<Folders.TypesForChange>
    {
        public Folders()
        {
            InitializeComponent();
        }

        public enum TypesForChange
        {
            SelectedFolders
        }

        private static List<TypesForChange> changed = new List<TypesForChange>();
        public event OnChangeDelegate OnChangeEvent;

        public TypesForChange AddNameOfChanged
        {
            set
            {
                if (!changed.Contains(value))
                {
                    changed.Add(value);
                }
                if (OnChangeEvent != null)
                {
                    OnChangeEvent();
                }
            }
        }

        public List<TypesForChange> ListOfChanged
        {
            get
            {
                return changed;
            }
        }

        public StringCollection SelectedFolders
        {
            get
            {
                return this.folderTree.SelectedDirectories;
            }
        }

        public void RemoveChanged()
        {
            changed.Clear();
        }

        private void Folders_Load(object sender, EventArgs e)
        {
            #region FolderTreeViewInit
            Raccoom.Windows.Forms.TreeViewFolderBrowserDataProviderShell32 shell32provider = new Raccoom.Windows.Forms.TreeViewFolderBrowserDataProviderShell32();
            shell32provider.EnableContextMenu = false;
            shell32provider.ShowAllShellObjects = true;
            folderTree.DataSource = shell32provider as Raccoom.Windows.Forms.ITreeViewFolderBrowserDataProvider;
            folderTree.RootFolder = Environment.SpecialFolder.MyComputer;
            folderTree.Populate(Environment.SpecialFolder.MyComputer);
            #endregion

            changed = new List<TypesForChange>();
        }

        private void folderTree_SelectedDirectoriesChanged(object sender, Raccoom.Windows.Forms.SelectedDirectoriesChangedEventArgs e)
        {
            AddNameOfChanged = TypesForChange.SelectedFolders;
        }

        private void folderTree_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                TreeNode node = folderTree.GetNodeAt(e.X, e.Y);
                if (node != null)
                {
                    folderTree.SelectedNode = node;
                    if (node.Checked == true)
                    {
                        selectWithSubfoldersToolStripMenuItem.Enabled = false;
                        selectOnlyThisFolderToolStripMenuItem.Enabled = false;
                        deselectWithSubfoldersToolStripMenuItem.Enabled = true;
                        deselectOnlyThisFolderToolStripMenuItem.Enabled = true;
                    } else {
                        selectWithSubfoldersToolStripMenuItem.Enabled = true;
                        selectOnlyThisFolderToolStripMenuItem.Enabled = true;
                        deselectWithSubfoldersToolStripMenuItem.Enabled = false;
                        deselectOnlyThisFolderToolStripMenuItem.Enabled = false;
                    }
                    folderTree.ContextMenuStrip.Show(Form.MousePosition.X, Form.MousePosition.Y);
                }
                
            }
        }

        private void selectWithSubfoldersToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (folderTree.SelectedNode.Checked == false)
            {
                folderTree.SelectedNode.Checked = true;
            }
            
        }

        private void selectOnlyThisFolderToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (folderTree.SelectedNode.Checked == false)
            {
                folderTree.CheckboxBehaviorMode = Raccoom.Windows.Forms.CheckboxBehaviorMode.SingleChecked;
                folderTree.SelectedNode.Checked = true;
                folderTree.CheckboxBehaviorMode = Raccoom.Windows.Forms.CheckboxBehaviorMode.RecursiveChecked;
            }
        }

        private void deselectWithSubfoldersToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (folderTree.SelectedNode.Checked == true)
            {
                folderTree.SelectedNode.Checked = false;
            }
            
        }

        private void deselectOnlyThisFolderToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (folderTree.SelectedNode.Checked == true)
            {
                folderTree.CheckboxBehaviorMode = Raccoom.Windows.Forms.CheckboxBehaviorMode.SingleChecked;
                folderTree.SelectedNode.Checked = false;
                folderTree.CheckboxBehaviorMode = Raccoom.Windows.Forms.CheckboxBehaviorMode.RecursiveChecked;
            }
        }
    }
}
